<template>
	<scroll-view class="tab bgw pd30" scroll-y="true">
		<view class="commonlyItem" v-for="i in list" @click="getChoose(i.jixing,i.typestr,i.arr)">
			<view class="title">{{i.title}}</view>
			<view class="commonly"><text class="text">{{i.typestr}}</text><text
					v-for="item in i.arr">{{item.name}}{{item.num}}{{item.danwei}};</text></view>
			<view class="uniFlex">
				<view @click.stop="edit(i)" class="last">编辑</view>
				<view @click.stop="del(i.id)" class="last last2">删除</view>
			</view>
		</view>
		<view class="based" v-if="hidden">暂未添加处方</view>

		<!-- <view class="commonlyItem">
			<view class="title">养血温润肠方</view>
			<view class="commonly">
				<text>中华饮片</text>何首乌60.0g，当归70.0g，生白芍90.0g，何60.0g，当归70.0g，首乌60.0g，当归70.0g，生白芍90.0g，何60.0g，当归70.0g生白芍90.0g，何首90…
			</view>
		</view> -->
		<drag-button :isDock="true" @btnClick="btnClick" />
	</scroll-view>
</template>

<script>
	import {
		cyf,
		cyf_delete
	} from '../../../request/api.js'
	import dragButton from "@/components/drag-button/drag-button.vue";
	export default {
		name: "tab22",
		components: {
			dragButton
		},
		data() {
			return {
				list: [],
				hidden: false
			};
		},

		// 组件里要使用vue的生命周期函数 mounted
		mounted() {
			this.getList()
		},
		watch: {
			$route(to, from) {
				// console.log("到哪个页面去", to.path);
				// console.log("从哪个页面来", from.path);
				if (from.path == "/pages/prescription/add") {
					console.log(from.path)
					this.getList()
				}
			}
		},
		methods: {
			// 编辑
			edit(items) {
				uni.navigateTo({
					url: '/pages/prescription/add?items=' + JSON.stringify(items)
				})
			},
			// 删除
			del(id) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该处方？',
					success: function(res) {
						if (res.confirm) {
							cyf_delete({
								id: id
							}).then(res => {
								uni.showToast({
									title: '操作成功',
									icon: "none"
								})
								that.list = [];
								that.getList();
							})
						}
					}
				});
			},
			// 获取用户信息
			getList() {
				console.log('开始获取数据')
				cyf().then(res => {
					if (res.data.length == 0) {
						this.hidden = true
					} else {
						this.list = res.data
						this.hidden = false
					}
				})
			},
			getChoose(rp, name, arr) {
				// console.log(rp,name,arr)
				let pages = getCurrentPages(); // 当前页面
				let beforePage = pages[pages.length - 2]; // 前一个页面
				let lists = arr.map(function(item) {
					return {
						id: item.yid,
						danwei: item.danwei,
						value: item.name,
						yongfaId: '',
						yongfaName: '',
						num: item.num
					}
				});
				this.$nextTick(() => {
					if (beforePage.route == 'pages/prescription/search2') {
						beforePage.$vm.arrayId1 = rp, //替换上个页面参数
							beforePage.$vm.arrayName1 = name, //替换上个页面参数
							beforePage.$vm.list = lists, //替换上个页面参数
							uni.navigateBack()
					}
				})
			},
			btnClick() {
				console.log('添加经典方')
				uni.navigateTo({
					url: './add?type=2'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tab {
		padding-bottom: 30upx;

		.commonlyItem {
			padding: 40upx 0;
			border-bottom: 2upx solid #E5E5E5;

			.title {
				font-size: 32upx;
				font-family: Source Han Sans CN;
				font-weight: bold;
				margin-bottom: 20upx;
			}

			.commonly {
				width: 100%;
				font-size: 28upx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				line-height: 56upx;
				color: #7B7B7B;

				.text {
					background: #F4F4F6;
					// display: inline-block;
					padding: 6upx 8upx;
					font-size: 24upx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #9F9F9F;
					margin-right: 10upx;
				}

			}

			.last {
				color: #6996FE;
				font-size: 24upx;
				border: 1px solid #6996FE;
				border-radius: 26upx;
				padding: 10upx 30upx;
				width: 120upx;
				line-height: 100%;
				margin-top: 30upx;
			}

			.last2 {
				margin-left: 20upx;
				color: #F86C53;
				border: 1px solid #F86C53;
			}
		}

		.based {
			text-align: center;
			padding-top: 30upx;
			font-size: 30rpx;
		}
	}
</style>
